package net.entelijan.cobean.examples.showcase.bind.table;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JOptionPane;

import net.entelijan.cobean.bind.impl.BoundPropBinding;
import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.core.ISingleSelectionListCobean;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitBook;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.cobean.table.DefaultSingleSelectionTableInitializer;
import net.entelijan.scf.IShowcase;

public class TableShowcase implements IShowcase<TablePanel> {

	private ILitService service = null;

	private SingleSelectionModel model = new SingleSelectionModel();

	public TableShowcase() {
		super();
	}

	@Override
	public void init(final TablePanel view) {

		configure(view);

		// Set some books to the model. As they are bound to the JTable they
		// appear (automatically) on the GUI.
		model.setBooks(service.loadAllBooks());

		view.getShowAllBooks().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(view, ExamplesUtil.listToString(
						model.getBooks(), 100));
			}

		});

		view.getShowSelecedBook().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(view, model.getSelectedBook());
			}

		});

		view.getSelectFirstBookButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				model.setSelectedBook(model.getBooks().get(0));
			}

		});

		view.getSelectThirdBookButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				model.setSelectedBook(model.getBooks().get(2));
			}

		});

		view.getDeleteBookButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				LitBook selBook = model.getSelectedBook();
				if (selBook == null) {
					JOptionPane.showMessageDialog(view, "No book is selected!");
				} else {
					List<LitBook> books = model.getBooks();
					books.remove(selBook);
				}
			}

		});

	}

	/**
	 * Creates all the cobeans and binds them to properties of 'model'
	 * 
	 * @param view
	 *            A GUI Panel containing all the components to be bound.
	 */
	private void configure(final TablePanel view) {
		{
			DefaultSingleSelectionTableInitializer<LitBook> ini = new DefaultSingleSelectionTableInitializer<LitBook>();
			ini.setCoTable(view.getTable());
			ini.setColumnDescs(ColumnDesc.createFilterableSortableColumnDescs(
					"title,Title", "year,Year", "author.name,Author"));
			ini.setCoFilterInput(view.getFilterText());
			ISingleSelectionListCobean<LitBook> cobean = ini.initialize();

			// Bind properties of the component bean to the model.
			BoundPropBinding selBpb = new BoundPropBinding();
			selBpb.setModelPath("selectedBook");
			selBpb.setCompProperty("selectedValue");
			model = selBpb.bind(model, cobean);

			BoundPropBinding dataBpb = new BoundPropBinding();
			dataBpb.setModelPath("books");
			dataBpb.setCompProperty("value");
			model = dataBpb.bind(model, cobean);
		}
	}


	public SingleSelectionModel getModel() {
		return model;
	}

	public void setModel(SingleSelectionModel model) {
		this.model = model;
	}

	public ILitService getService() {
		return service;
	}

	public void setService(ILitService service) {
		this.service = service;
	}

}
